package utils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

import org.apache.commons.lang.RandomStringUtils;

public class StringUtil implements Serializable {

	private static final long serialVersionUID = 1L;

	// GUID
	public static String guid() {
		return UUID.randomUUID().toString().toUpperCase().replaceAll("-", "");
	}

	// 随机位数
	public static String number(int scale) {
		return RandomStringUtils.randomNumeric(scale);
	}

	// 格式化对象避免null的情况
	public static String space(Object source) {
		return (isEmpty(source)) ? "" : source.toString();
	}

	// 括号起指定字符串
	public static String quote(Object source) {
		return (isEmpty(source)) ? "" : "[" + source.toString() + "]";
	}

	// 是否为空
	public static boolean isEmpty(Object source) {
		return (source == null || source.toString().length() == 0);
	}

	// 是否整数
	public static boolean isDigit(String text) {
		return RegularUtil.test(text, RegularUtil.NUM);
	}

	// 是否浮点数
	public static boolean isNumeric(String text) {
		try {
			if (new BigDecimal(text).scale() != 2) {
				return false;
			}
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	// 是否是汇率
	public static boolean isRate(String text) {
		try {
			if (new BigDecimal(text).scale() != 4) {
				return false;
			}
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	// 是否日期
	public static boolean isDate(String text) {
		try {
			new SimpleDateFormat("yyyy-MM-dd").parse(text);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	// 填充字符串
	public static String pad(String src, int len, char ch, boolean reverse) {
		int diff = len - src.length();
		if (diff <= 0) {
			return src;
		}
		char[] charr = new char[len];
		if (reverse) {
			System.arraycopy(src.toCharArray(), 0, charr, 0, src.length());
			for (int i = src.length(); i < len; i++) {
				charr[i] = ch;
			}
		} else {
			System.arraycopy(src.toCharArray(), 0, charr, diff, src.length());
			for (int i = 0; i < diff; i++) {
				charr[i] = ch;
			}
		}
		return new String(charr);
	}

	// 按照逗号分割字符串
	public static String[] split(String source) {
		if (isEmpty(source)) {
			return null;
		}
		return source.trim().split(",");
	}

	// 按照逗号分割字符串
	public static Set<String> toSet(String source) {
		if (isEmpty(source)) {
			return new HashSet<String>();
		}
		Set<String> set = new HashSet<String>();
		String[] items = split(source);
		for (String item : items) {
			if (!isEmpty(item)) {
				set.add(item.trim());
			}
		}
		return set;
	}

	// 去空格
	public static String trim(String source) {
		if (isEmpty(source)) {
			return source;
		}
		return source.trim();
	}

	// 中间模糊
	public static String fake(String source, int top, int end) {
		if (isEmpty(source)) {
			return source;
		}
		if (top <= 0 || end <= 0) {
			return source;
		}
		if (source.length() < (top + end)) {
			return source;
		}
		String theTop = source.substring(0, top);
		String theEnd = source.substring(source.length() - end, source.length());
		if (source.length() > (top + end)) {
			theTop = pad(theTop, source.length() - end, '*', true);
		}
		if (source.length() == (top + end)) {
			theTop = theTop + "*";
		}
		return theTop + theEnd;
	}
	
	// 只保留字符串的中英文和下划线
	public static String concise(String source) {
		if (isEmpty(source)) {
			return source;
		}
		return source.replaceAll("[^0-9a-zA-Z\u4e00-\u9fa5_]+","");
	}

	// 去除字符串中的特殊符号
	public static String simple(String source) {
		if (isEmpty(source)) {
			return source;
		}
		String result = source.replaceAll("\"", "").replaceAll("\\'", "").replaceAll("/", "").replaceAll("<", "").replaceAll(">", "").replaceAll("\\(", "").replaceAll("\\)", "").replaceAll("\\+", "").replaceAll("\\{", "")
				.replaceAll("}", "").replaceAll("\n", "").replaceAll("\r\n", "").replaceAll("\\+", "").replaceAll("\\*", "").replaceAll("\\.", "").replaceAll("#", "").replaceAll("$", "").replaceAll("%", "").replaceAll("^", "")
				.replaceAll("&", "").replaceAll("!", "").replaceAll("@", "").replaceAll("~", "").replaceAll("`", "").replaceAll(";", "").replaceAll(",", "").trim();
		return result;
	}

	// 字符串不能超过指定长度
	public static String limit(String source, int len) {
		if (isEmpty(source)) {
			return source;
		}
		if (source.length() <= len) {
			return source;
		}
		return source.substring(0, len) + "...";
	}

	// 从字符串中某个字符位置截取后半部分
	public static String removeStart(String source, String target) {
		int i = source.indexOf(target);
		String res = source.substring(i + 1, source.length());
		return res;
	}

	// 从字符串中某个字符位置截取前半部分
	public static String removeLast(String source, String target) {
		int i = source.lastIndexOf(target);
		if (i > 0) {
			return source.substring(0, i);
		} else {
			return source;
		}
	}

	// 将字符串转为下划线分隔
	public static String transferUnder(String name) {
		if (isEmpty(name)) {
			return "";
		}
		StringBuilder result = new StringBuilder();
		result.append(name.substring(0, 1).toLowerCase());
		for (int i = 1; i < name.length(); i++) {
			String s = name.substring(i, i + 1);
			String slc = s.toLowerCase();
			if (!s.equals(slc)) {
				result.append("_").append(slc);
			} else {
				result.append(s);
			}
		}
		return result.toString();
	}

	// 将下划线字符串转为驼峰风格
	public static String transferCamel(String name) {
		if (isEmpty(name)) {
			return "";
		}
		StringBuilder result = new StringBuilder();
		for (int i = 0; i < name.length(); i++) {
			String s = name.substring(i, i + 1);
			if (s.equals("_")) {
				i++;
				result.append(name.substring(i, i + 1).toUpperCase());
			} else {
				result.append(s);
			}
		}
		return result.toString();
	}

	// 首字符大写
	public static String upperHead(String name) {
		if (isEmpty(name)) {
			return "";
		}
		return name.substring(0, 1).toUpperCase() + name.substring(1);
	}

	// 首字符小写
	public static String lowerHead(String name) {
		if (isEmpty(name)) {
			return "";
		}
		return name.substring(0, 1).toLowerCase() + name.substring(1);
	}

	public static void main(String[] args) throws Exception {
		String str="123.jjdi#dl王浦侠'哈@+#//._,[]{}~`!@#$%^&*()哈\"\"";
		
		System.out.println(concise(str));
	}

}
